package it.koutifaris.travianmap.dal.sqlservices.travianserver;

import it.koutifaris.travianmap.dal.TravianServerService.TravianServer;
import it.koutifaris.travianmap.dal.TravianServerSettingsService;
import it.koutifaris.travianmap.dal.sqlservices.travianserver.TravianServerContract.Settings;
import it.koutifaris.travianmap.utils.cursor.ColumnNameCursor;
import it.koutifaris.travianmap.utils.cursor.ColumnNameCursorWrapper;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class SqlTravianServerSettingsService implements TravianServerSettingsService {

	private final SQLiteDatabase db;

	public SqlTravianServerSettingsService(SQLiteDatabase db) {
		this.db = db;
	}

	@Override
	public void saveSettings(TravianServerSettings settings) {
		insertIntoServers(settings);
		insertIntoSettints(settings);
	}

	private void insertIntoServers(TravianServerSettings settings) {
		SqlTravianServerService sqlTravianServerService = new SqlTravianServerService(db);
		sqlTravianServerService.newServer(settings.getServer());
	}

	private void insertIntoSettints(TravianServerSettings settings) {
		ContentValues values = createContentValuesForSettings(settings);
		db.insertWithOnConflict(Settings.TABLE_NAME, null, values, SQLiteDatabase.CONFLICT_REPLACE);
	}

	private ContentValues createContentValuesForSettings(TravianServerSettings settings) {
		ContentValues values = new ContentValues();

		values.put(Settings.COLUMN_NAME_SERVER, settings.getServer());
		values.put(Settings.COLUMN_NAME_AUTOUPDATE, settings.isAutoUpdate());
		values.put(Settings.COLUMN_NAME_WIFI_ONLY, settings.isWifiOnly());

		return values;
	}

	@Override
	public TravianServerSettings getSettginsFor(String server) {
		String[] columns = { Settings.COLUMN_NAME_SERVER, Settings.COLUMN_NAME_AUTOUPDATE, Settings.COLUMN_NAME_WIFI_ONLY };

		String selection = Settings.COLUMN_NAME_SERVER + " = ?";
		String[] selectionArgs = { server };
		Cursor cursor = db.query(Settings.TABLE_NAME, columns, selection, selectionArgs, null, null, null);

		TravianServerSettings result = null;

		if (cursor.moveToNext()) {
			result = getSettingsFromCursor(cursor);
		}

		cursor.close();

		return result;
	}

	private TravianServerSettings getSettingsFromCursor(Cursor cursor) {
		ColumnNameCursor columnNameCursor = new ColumnNameCursorWrapper(cursor);

		TravianServerSettings result = TravianServerSettingsBuilder.settingsBuilder()
				.setServer(columnNameCursor.getString(Settings.COLUMN_NAME_SERVER))
				.setAutoUpdate(getBooleanFromCursor(columnNameCursor, Settings.COLUMN_NAME_AUTOUPDATE))
				.setWifiOnly(getBooleanFromCursor(columnNameCursor, Settings.COLUMN_NAME_WIFI_ONLY))
				.build();

		return result;
	}

	private boolean getBooleanFromCursor(ColumnNameCursor cursor, String columnName) {
		int val = cursor.getInt(columnName);
		return (val != 0);
	}

	@Override
	public int deleteAllSettings() {
		return db.delete(Settings.TABLE_NAME, "1", null);
	}

	@Override
	public boolean deleteSettings(TravianServer server) {
		String whereClause = Settings.COLUMN_NAME_SERVER + "= ?";
		String[] whereArgs = { server.getServerId() };
		int numRowsDeleted = db.delete(Settings.TABLE_NAME, whereClause, whereArgs);

		return numRowsDeleted > 0;
	}

	@Override
	public void close() {
		db.close();
	}
}
